<?
    cria_variaveis($_POST,$_GET,$_FILES);
    require_once("./classes/upload.php"); 
?> 
<p class="titulo" align="center"><?php echo $msg->get('GAUSS_SEIDEL_MSG_02') ?></p>
<p class="subtitulo" align="center"><?php echo $msg->get('GAUSS_SEIDEL_MSG_03') ?></p>
<p>
<div class="tabbar">
    <ul>
    <li><a href="index.php?inc=gauss-seidel.php&modo=material"<?php if ($_SESSION['ANAWEB']->RetornaModoExecucao() == "material") echo " class=\"tabact\""; ?>><?php echo $msg->get('Material_Teorico') ?></a></li>
    <li><a href="index.php?inc=gauss-seidel.php&modo=exemplo"<?php if ($_SESSION['ANAWEB']->RetornaModoExecucao() == "exemplo") echo " class=\"tabact\""; ?>><?php echo $msg->get('Exemplos_Prontos') ?></a></li>
    <li><a href="index.php?inc=gauss-seidel.php&modo=teclado"<?php if ($_SESSION['ANAWEB']->RetornaModoExecucao() == "teclado") echo " class=\"tabact\""; ?>><?php echo $msg->get('Entrada_Teclado') ?></a></li>
    </ul>
</div>
<div id="cas">
<?php
if (!isset($Calcular))
{
    if ($_SESSION['ANAWEB']->RetornaModoExecucao() == "material")
    {
        $uploads = new Upload();
        $uploads->Lista("sistemas");
    }
    else if ($_SESSION['ANAWEB']->RetornaModoExecucao() == "exemplo")
    {
        include "exibe_prob_sela_gauss-seidel_xml.php"; // exibe menu de exemplos contidos na biblioteca de exemplos em xml
    }
    else if ($_SESSION['ANAWEB']->RetornaModoExecucao() == "teclado")
    {
        if ((strlen($dim)==0) | ($dim<=0) | (strlen($k)==0) | ($k<=0) | (strlen($t)==0) | ($t<=0) )
        {
            echo "<form method=post enctype=multipart/form-data action=index.php?inc=gauss-seidel.php>";
            echo $msg->get('GAUSS_SEIDEL_MSG_04');
            echo "<input type=text size='2' maxlength='4' name=dim value=".($dim).">";
            echo "<br><br>";
            echo $msg->get('GAUSS_SEIDEL_MSG_05');
            echo "<input type=text size='2' maxlength='4' name=k value=".($k).">";
            echo "<br><br>";
            echo $msg->get('GAUSS_SEIDEL_MSG_06');
            echo "<input type=text size='2' maxlength='4' name=t value=".($t).">";
            echo "<br><br>";
            echo "<input type=hidden name=executar value=sim>";
            echo "<input class=button type=submit name=Submit value=".$msg->get('G_MSG_02').">";
            echo "</form>"; 
            echo "<form method=post enctype=multipart/form-data action=index.php?inc=gauss-seidel.php>";
            echo "<input class=button type=submit name=Submit value=".$msg->get('G_MSG_03').">";
            echo "</form>";
        }
        else
        { 
            echo "<form method=post enctype=multipart/form-data action=index.php?inc=gauss-seidel.php>";
            echo $msg->get('GAUSS_SEIDEL_MSG_04');
            echo "<input type=text size='2' maxlength='4' name=dim value=".($dim).">";
            echo "<br><br>";
            echo $msg->get('GAUSS_SEIDEL_MSG_05');
            echo "<input type=text size='2' maxlength='4' name=k value=".($k).">";
            echo "<br><br>";
            echo $msg->get('GAUSS_SEIDEL_MSG_07');
            echo "<input type=text size='2' maxlength='4' name=t value=".($t).">";
            echo "<br><br>";
            echo "<p align=left><font face=Arial>".$msg->get('GAUSS_SEIDEL_MSG_23')."</font>";
            echo "<nobr>";
            for ($i=0; $i<$dim; $i++) {
                echo "<br><br>";
                for ($j=0; $j<$dim; $j++) {
                        $aux=$j+1;
                    echo "<input type=text maxlength=10 size=5 name=A[".($i)."][".($j)."] value=".($A[$i][$j])."> X <sub> ".($aux)." </sub>";
                        if ($j<($dim-1)) {
                        echo " + ";
                    } //end if
                } //end for j
                echo " = ";
                echo "<input type=text maxlength=10 size=5 name=T[".($i)."] value=".($T[$i]).">";
            } //end for i
            echo "</nobr>";
            echo "<p align=left><font face=Arial>".$msg->get('GAUSS_SEIDEL_MSG_08')."";
            echo "<br><br>";
            echo "<nobr>";
            for ($i=0; $i<$dim; $i++) {
                $aux=$i+1;
                echo "X <sub> ".($aux)."</sub> -> ";
                echo "<input type=text maxlength=10 size=5 name=vtx[".($i)."] value=".($vtx[$i]).">";
                echo "<br><br>";
            } //end for i
            echo "</nobr>";
            echo "<p>";
            echo "<input class=button type=submit name=Calcular value=".$msg->get('G_MSG_04').">";
            echo "<br>";
            echo "</form>";
            echo "<form method=post enctype=multipart/form-data action=index.php?inc=gauss-seidel.php>";
            echo "<input class=button type=submit name=Submit value=".$msg->get('G_MSG_03').">";
            echo "</form>";
        } //end if
        echo "</body>";
	}
}
elseif (checaNumerico($dim) && checaNumerico($A) && checaNumerico($T) && checaNumerico($vtx) && checaNumerico($k) && checaNumerico($t)) {
    if ($_SESSION['ANAWEB']->RetornaModoExecucao() == "exemplo")
        verificaBib($xml_file);
    echo "<br>".$msg->get('GAUSS_SEIDEL_MSG_04')." ".($dim)."<br>";
    echo "".$msg->get('GAUSS_SEIDEL_MSG_05')." ".($k)."<br>";
    echo "".$msg->get('GAUSS_SEIDEL_MSG_24').": 10 <sup>-<font size=2> ".($t)."</sup></font><br><br>";
    echo "<p align=left><font face=Arial><b>".$msg->get('GAUSS_SEIDEL_MSG_23')."</b></font><br>";
    echo "<nobr>";
    for ($i=0; $i<$dim; $i++) {
        echo "<br>";
        for ($j=0; $j<$dim; $j++) {
            $aux=$j+1;
            if ($j>0) {
                if ($A[$i][$j-1]!=0) {
                    if ($A[$i][$j]>0) {
                        echo (" + ");
                    } //end if
                    else {
                        if ($j<($dim-1)) {
                            if ((@$A[$i][$j]==0)and(@$A[$i][$j-2]!=0)) {
                                echo (" + ");
                            } //end if
                        } //end if
                    } //end else
                } //end ifA
            } //end ifj
            if (($A[$i][$j]!=0)and($A[$i][$j]!=1)) {
                echo $A[$i][$j];
            } //end if
            if ($A[$i][$j]!=0) {
                echo "x<sub>".($aux)."</sub>";
            } //end if
        } //end forj
        echo (" = "); 
        echo $T[$i];
    } //end fori
    echo "</nobr>";
    echo "<p align=left><font face=Arial>".$msg->get('GAUSS_SEIDEL_MSG_08')."";
    echo "<br><br>";
    echo "<nobr>";
    for ($i=0; $i<$dim; $i++) {
        $aux=$i+1;
        echo "X <sub> ".($aux)."</sub> -> ";
        echo "<input type=text  maxlength=10 size=5 name=vtx[".($i)."] value=".($vtx[$i])." readonly=readonly>";
        //echo "<br><br>";
        $Vtx[$i]=$vtx[$i];
    } //end for i
    echo "</nobr><Br><br>";
    /******************************************************************************/
    $aux=1;
    $num=10;
    for ($i=0; $i<$t; $i++) {
        $aux=$aux*$num;
    } //for
    $ddom=verificaConvergencia();
    $sass=sassenfeld($A,$dim);
    if (!$ddom && !$sass)
    {
        echo $msg->get('GAUSS_SEIDEL_MSG_25')."<br><br>";
    }
    $pode = 1;
    $num=(1/$aux);
    for ($i=0; $i<$dim; $i++) {
        if ($A[$i][$i]==0) {
        $pode=-1;
        } //if
    } //for
    $vezes=0;
    if ($pode==0) {
        echo "<p align=left><font face=Arial> ".$msg->get('GAUSS_SEIDEL_MSG_12')."";
        echo "<br>".$msg->get('GAUSS_SEIDEL_MSG_13')."";
        echo "<p>".$msg->get('GAUSS_SEIDEL_MSG_14')."";
        echo "<p>";
        echo "</form>";
        echo "<form method=post enctype=multipart/form-data action=index.php?inc=gauss-seidel.php>";
        echo "<input class=button type=submit name=Submit value=".$msg->get('G_MSG_03').">";
        echo "</form>";
    } //if
    else if ($pode==-1) {
        echo "<p align=left><font face=Arial> ".$msg->get('GAUSS_SEIDEL_MSG_15')."";
        echo "<br>".$msg->get('GAUSS_SEIDEL_MSG_16')."";
        echo "<p>".$msg->get('GAUSS_SEIDEL_MSG_14')."";
        echo "<p>";
        echo "</form>";
        echo "<form method=post enctype=multipart/form-data action=index.php?inc=gauss-seidel.php>";
        echo "<input class=button type=submit name=Submit value=".$msg->get('G_MSG_03').">";
        echo "</form>";
    } // else if
    for ($i=1; $i<$dim; $i++) {
        $vtd[$i]=$vtx[$i];
    } //for
    if ($pode==1) {
        echo "<br>".$msg->get('GAUSS_SEIDEL_MSG_17');
        echo "<br> <br>";
        echo "<table border=0 cellspacing=0>";
        echo "   <tr>";
        echo "    <td>".$msg->get("Iteracoes");
        echo "    </td>";
        for ($i=0; $i<$dim; $i++)
        {
            for ($j=0; $j<3; $j++)
            {
                if ($j == 0)
                {
                    echo "    <td>";
                    echo "      <p align=center> x".($i+1)." </p>";
                    echo "    </td>";
                }
                else if ($j == 1)
                {
                    echo "    <td>";
                    echo "      <p align=center> ".$msg->get("Erro_Relativo_De")."".($i+1)." </p>";
                    echo "    </td>";
                }
                else if ($j == 2)
                {
                    echo "    <td>";
                    echo "      <p align=center> DIGSE<br>de x".($i+1)." </p>";
                    echo "    </td>";
                }
            } // for j
        } // for i
        echo "   </tr>";
        while ((@$chegou!=1)and($vezes<=$k)) {
            $vezes=$vezes+1;
            for ($i=0; $i<$dim; $i++) {
                for ($j=0; $j<$dim; $j++) {
                    if ($i!=$j) {
                        @$vtemp[$i]+=$A[$i][$j]*$vtx[$j];
                    } //if
                } //forj
                $vtemp[$i]-=$T[$i];
                $vtemp[$i]*=(-1/$A[$i][$i]);
                $vtx[$i]=$vtemp[$i];
            } //fori
            $chegou=1;
            for ($i=0; $i<$dim; $i++) {
                if (abs(@$vtd[$i]) != 0)
                    $err_rel[$i]=abs($vtd[$i]-$vtemp[$i])/abs($vtd[$i]);
                else if (abs($vtemp[$i]) != 0)
                    $err_rel[$i]=abs(@$vtd[$i]-$vtemp[$i])/abs($vtemp[$i]);
                if ($err_rel[$i]>$num)
                {
                    $chegou=0;
                } //if
            } //for
            //Imprime resultados parciais em tabela
            echo "<tr>";
            echo "<td>";
            echo "".($vezes)." <sup>a</sup>".$msg->get('GAUSS_SEIDEL_MSG_18')."";
            echo "</td>";
            for ($i=0; $i<$dim; $i++) {
                echo "<td>";
                echo "<input type=text name=vtx[".($i)."] value=".($vtx[$i])." maxlength=100 size=5 readonly=readonly>";
                echo "</td>";
                echo "<td>";
                if ($vezes == 1) {
                    //echo "<input type=text name=erro[".($i)."]  maxlength=100 size=7 value='-' readonly=readonly>";
                    echo "-";
                } else {
                    //echo "<input type=text name=erro[".($i)."]  maxlength=100 size=7 value=".$err_rel[$i]." readonly=readonly>";
                    if ($err_rel[$i]>$num) echo "<font color=red>".round($err_rel[$i],($t+1))."</font>";
                    else echo round($err_rel[$i],($t+1));
                }
                echo "</td>";
                echo "<td>";
                //$mi = 0.5 * bcpow(2,-126,127); 	
                $mi = 0.5 * round(pow(2,-126),127); // ** substituido com funcoes sem a biblioteca bcmath - podem haver erros **
                $digse[$i] = - ( 0.3 + log10 ($mi + $err_rel[$i]));
                //echo "<input type=text name=digse[".($i)."] value=".($digse[$i])." size=5 maxlength=100 readonly=readonly>";
                echo round($digse[$i],($t+1));
                echo "</td>";
            } // end for
            echo "</tr>";
            for ($i=0; $i<$dim; $i++) {
                $vtd[$i]=$vtx[$i];
            } //for
            for ($i=0; $i<$dim; $i++) {
                $vtemp[$i]=0;
            } //for
        } //while
        echo "</table>";
        if ($chegou==1) {
            echo "<br>";
            echo "<p align=left><font face=Arial>".$msg->get('GAUSS_SEIDEL_MSG_19')." ".($vezes)." &nbsp ".$msg->get('GAUSS_SEIDEL_MSG_20')." </font>";
            echo "<p>";
            echo "<p align=left><font face=Arial> <b>".$msg->get('GAUSS_SEIDEL_MSG_21')."</b> </font>";
            echo "<br>";
            echo "x = (";
            for ($i=0; $i<$dim; $i++) {
                echo $vtx[$i];
                if ($i!=$dim-1) {echo ",";} 
                echo "<input type=hidden name=vtx[".($i)."] maxlength=10 size=5 value=".($vtx[$i]).">";
            } //end fori
            echo ") <sup>T</sup>";
            seqlsol($A,$vtx,$dim,$T,$num);
        } //if chegou
        else {
            echo "<p align=left><font face=Arial>".$msg->get('GAUSS_SEIDEL_MSG_10')."</font>";
            echo "<p>";
            echo "<form method=post enctype=multipart/form-data action=index.php?inc=gauss-seidel.php>";
            echo "<input type=hidden name=executar value=sim>";
            echo "<input type=hidden name=dim value=".($dim).">";
            echo "<input type=hidden name=k value=".($k).">";
            echo "<input type=hidden name=t value=".($t).">";
            for ($i=0; $i<$dim; $i++) {   
                    for ($j=0; $j<$dim; $j++) {
                echo "<input type=hidden name=A[".($i)."][".($j)."] value=".($A[$i][$j]).">";
                    }
                echo "<input type=hidden name=T[".($i)."] value=".($T[$i]).">";
            } 
            echo "<input class=button type=submit name=Submit value=".$msg->get('GAUSS_SEIDEL_MSG_22').">";
            echo "</form>";
        } //else
        echo "<p>";
        echo "<form method=post enctype=multipart/form-data action=index.php?inc=gauss-seidel.php>";
        echo "<input class=button type=submit name=Submit value=".$msg->get('G_MSG_03').">";
        echo "</form>";
    } //if
    echo $msg->get('Verifique_Outros_Metodos') ;
    echo "<p>";
    echo "<form method=post enctype=multipart/form-data action=index.php?inc=jacobi.php>";
    echo "<input type=hidden name=executar value=sim>";
    echo "<input type=hidden name=dim value=".($dim).">";
    echo "<input type=hidden name=k value=".($k).">";
    echo "<input type=hidden name=t value=".($t).">";
    for ($i=0; $i<$dim; $i++) {
            for ($j=0; $j<$dim; $j++) {
        echo "<input type=hidden name=A[".($i)."][".($j)."] value=".($A[$i][$j]).">";
            }
        echo "<input type=hidden name=T[".($i)."] value=".($T[$i]).">";
    }
    for ($i=0; $i<$dim; $i++) {
        echo "<input type=hidden name=vtx[".($i)."] value=".($Vtx[$i]).">";
    } //end for i
    if (isset($_POST["navegacao"]) && $_POST["navegacao"] == "sim")
    {
        echo "<input type=\"hidden\" name=\"xml_file\" value=\"$xml_file\">";
        echo "<input type=\"hidden\" name=\"navegacao\" value=\"sim\">";
    }
    echo "<input class=button type=submit name=Calcular value='".$msg->get('JACOBI_MSG_03')."'>";
    echo "</form>";
    if (isset($_POST["navegacao"]) && $_POST["navegacao"] == "sim")
    {
        echo "<form name=\"exemplos\" enctype=\"multipart/form-data\" method=\"post\">";
        echo "<script language=\"javascript\" type=\"text/javascript\">\n";
        echo "function criarLinks(value) {\n";
        echo "document.exemplos.indice.value = value;\n";
        echo "document.exemplos.action = 'index.php?inc=gauss-seidel.php&modo=exemplo'\n";
        echo "document.exemplos.submit();\n";
        echo "}\n";
        echo "</script>";
        echo "<input type=\"hidden\" name=\"id_bib\" value=\"".@$id_bib."\">";
        echo "<input type=\"hidden\" name=\"xml_file\" value=\"".$xml_file."\">";
        echo "<input type=\"hidden\" name=\"indice\">";
        echo $msg->get('Outros_Exemplos')."<br>";
        $objeto = DOMDocument::load($xml_file);
        $filho = $objeto->getelementsbytagname("lin_systems");
        if ($filho->length) {
            $temp = $filho->item(0)->getelementsbytagname("system");
            $j = 0;
            for($i = 0; $i < $temp->length; $i++)
            {
                $method = $temp->item($i)->getelementsbytagname("method");
                $conteudo = $method->item(0)->nodeValue;
                $dime = $temp->item($i)->getelementsbytagname("dim");
                $dime = $dime->item(0)->nodeValue;
                if ($conteudo == 'iterative') {
                    if ($j != 0) echo " | ";
                    if ($j == @$indice) echo "<b>";
                    echo "<a class=\"bibliotecaexemplos\" href=\"javascript:criarLinks($j)\" title=\"";
                    echo "Dimens&atilde;o $dime\"><u>".($j+1)."</u></a>";
                    if ($j == @$indice) echo "</b>";
                    $j++;
                }
            }
        }
        echo "<br>";
    }
}
else
{
?>
        <p><font color="red"><?php echo $msg->get('Apenas_Dados_Numericos') ?></font></p>
        <p><input type="button" value="<?php echo $msg->get('Voltar') ?>" onclick="history.back()" class="button"></p>
<?php
}
?>
</div>